import Vue from 'vue'
import VueRouter from 'vue-router'

// import Home from '@/components/Home.vue'
// import Welcome from '@/components/Welcome.vue'
// import Users from '@/components/user/Users.vue'
// import Rights from '@/components/power/Rights.vue'
// import Roles from '@/components/power/Roles.vue'
// import Cate from '@/components/goods/Cate.vue'
// import Params from '@/components/goods/Params.vue'
// import List from '@/components/goods/List.vue'
// import Add from '@/components/goods/Add.vue'
// import Order from '@/components/Order/Order.vue'
// import Report from '@/components/Report/Report.vue'

// 导入需要的组件 -- 使用路由懒加载的形式
// import Login from '@/components/Login.vue'
const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '@/components/Login.vue')
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '@/components/Home.vue')
const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '@/components/Welcome.vue')
const Users = () => import(/* webpackChunkName:"user" */ '@/components/user/Users.vue')
const Rights = () => import(/* webpackChunkName:"Right_Roles" */ '@/components/power/Rights.vue')
const Roles = () => import(/* webpackChunkName:"Right_Roles" */ '@/components/power/Roles.vue')
const Cate = () => import(/* webpackChunkName:"Cate_Params" */ '@/components/goods/Cate.vue')
const Params = () => import(/* webpackChunkName:"Cate_Params" */ '@/components/goods/Params.vue')
const List = () => import(/* webpackChunkName:"GoodsList_add" */ '@/components/goods/List.vue')
const Add = () => import(/* webpackChunkName:"GoodsList_add" */ '@/components/goods/Add.vue')
const Order = () => import(/* webpackChunkName:"order_report" */ '@/components/Order/Order.vue')
const Report = () => import(/* webpackChunkName:"order_report" */ '@/components/Report/Report.vue')

Vue.use(VueRouter)

const routes = [
  // 登录的规则
  { path: '/login', component: Login },
  // 路由重定向，访问根路径就自动跳转到登录界面
  { path: '/', redirect: '/login' },
  {
    path: '/home',
    component: Home,
    redirect: '/home/welcome',
    children: [
      { path: 'welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: List },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由前置导航守卫
router.beforeEach((to, from, next) => {
  // 如果用户访问的是登录页，直接放行
  if (to.path === '/login') return next()
  // 从 sessionStorage 中获取 token 值
  const token = sessionStorage.getItem('token')
  // 如果没有 token ，则强制返回登录页
  if (!token) return next('/login')
  // 有 token，则放行
  next()
})

export default router
